package com.example.controller;

import com.example.common.PageResult;
import com.example.common.Result;
import com.example.pojo.dto.GoodsQueryDto;
import com.example.pojo.entity.Goods;
import com.example.pojo.vo.GoodsQueryVo;
import com.example.service.GoodsService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 宠物用品 controller 层
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @PostMapping("/add")
    public Result add(@RequestBody Goods goods) {
        this.goodsService.add(goods);
        return Result.success();
    }

    @GetMapping("/pageQuery")
    public Result pageQuery(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            GoodsQueryDto dto
    ) {
        PageResult<GoodsQueryVo> pageResult = this.goodsService.pageQuery(pageNum, pageSize, dto);
        return Result.success(pageResult);
    }

    @GetMapping("/query")
    public Result queryById(@RequestParam Integer id) {
        GoodsQueryVo goods = this.goodsService.queryById(id);
        return Result.success(goods);
    }

    @GetMapping("/query/all")
    public Result queryAll(GoodsQueryDto dto) {
        List<GoodsQueryVo> goodsList = this.goodsService.queryAll(dto);
        return Result.success(goodsList);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Goods goods) {
        this.goodsService.update(goods);
        return Result.success();
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        this.goodsService.delete(id);
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        this.goodsService.deleteBatch(ids);
        return Result.success();
    }
}
